package com.linkai.ai.config;

import com.linkai.ai.constants.SystemConstants;
import com.linkai.ai.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {

    @Bean
    public ChatMemory chatMemory() {
        // 使用消息数量窗口（推荐简单场景）
        return MessageWindowChatMemory.builder()
                .maxMessages(20)  // 按需调整数量
                .build();
    }

    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }


    @Bean
    public ChatClient chatClient(OllamaChatModel model,ChatMemory chatMemory) {
        return ChatClient
                .builder (model)
                .defaultSystem ("你是一个热心、可爱的智能助手，你的名字叫超级宝贝JOJO，请以超级宝贝JOJO的身份和语气回答问题。")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }

    @Bean
    public ChatClient gamechatClient(OpenAiChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder (model)
                .defaultSystem (SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }
    @Bean
    public ChatClient serviceChatClient(OpenAiChatModel model, ChatMemory chatMemory, CourseTools courseTools) {
        return ChatClient
                .builder (model)
                .defaultSystem (SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultTools (courseTools)
                .build();
    }
    @Bean
    public ChatClient pdfChatClient(OpenAiChatModel model, ChatMemory chatMemory,VectorStore vectorStore) {
        return ChatClient
                .builder (model)
                .defaultSystem ("请根据上下文回答问题，遇到上下文没有的问题不要随意编造。")
                .defaultAdvisors (
                        new SimpleLoggerAdvisor (),
                        MessageChatMemoryAdvisor.builder (chatMemory).build (),
                        QuestionAnswerAdvisor.builder (vectorStore)
                                .searchRequest (SearchRequest.builder ()
                                        .similarityThreshold (0.6)
                                        .topK (2)
                                        .build ())
                                .build ()
                )
                .build ();
    }
}
